function [m]=get_m(ests,W,nobeta,nosigma)

%get weights
b_W_fs=cell2mat(W(1)); w_W_fs=cell2mat(W(3));
b_W_ss=cell2mat(W(2)); w_W_ss=cell2mat(W(4));

L=length(ests)/2;
pi_hat=ests(1:L,:);
delta_hat=ests(L+1:end,:);

pi_hat2=pi_hat.^2;
%if nobeta==0
    b_X=[ones(L,1),pi_hat(:,1),pi_hat2(:,1)];
    w_X=[ones(L,1),pi_hat(:,2),pi_hat2(:,2)];
%else
%    b_X=[ones(L,1),pi_hat(:,1)];   
%    w_X=[ones(L,1),pi_hat(:,2)]; 
%end
Y=delta_hat;

b_coef=(b_X'*b_W_ss*b_X)\(b_X'*b_W_ss*Y(:,1));
b_e=Y(:,1)-b_X*b_coef;
b_s=b_e.^2;
b_pi_mn=wmean(pi_hat(:,1),diag(b_W_fs));
b_r=pi_hat(:,1)-b_pi_mn;
b_t=b_r.^2;
w_coef=(w_X'*w_W_ss*w_X)\(w_X'*w_W_ss*Y(:,2));
w_e=Y(:,2)-w_X*w_coef;
w_s=w_e.^2;
w_pi_mn=wmean(pi_hat(:,2),diag(w_W_fs));
w_r=pi_hat(:,2)-w_pi_mn;
w_t=w_r.^2;

m=[b_coef;w_coef;b_pi_mn;w_pi_mn;sqrt(wmean(b_t,diag(b_W_fs)));sqrt(wmean(w_t,diag(w_W_fs)))];
%if nosigma==0
    m=[m;sqrt(wmean(b_s,diag(b_W_fs)));sqrt(wmean(w_s,diag(w_W_fs)))];
%end

c_W_fs=(b_W_fs.*w_W_fs).^0.5;
c_W_ss=(b_W_ss.*w_W_ss).^0.5;
m=[m;wmean(b_r.*w_r,diag(c_W_fs))/(sqrt(wmean(b_t,diag(b_W_fs)))*sqrt(wmean(w_t,diag(w_W_fs))))];
%if nosigma==0
    m=[m;wmean(b_e.*w_e,diag(c_W_ss))/(sqrt(wmean(b_s,diag(b_W_fs)))*sqrt(wmean(w_s,diag(w_W_fs))))];
%end